<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员用户管理界面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        h1 {
            color: #333;
            border-bottom: 2px solid #eee;
            padding-bottom: 10px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px 15px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: 600;
        }
        tr:hover {
            background-color: #f1f8fe;
        }
        button {
            padding: 8px 15px;
            margin-right: 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            background-color: #4a90e2;
            color: white;
            font-size: 14px;
        }
        button:hover {
            background-color: #357abd;
        }
        button.delete {
            background-color: #e74c3c;
        }
        button.delete:hover {
            background-color: #c0392b;
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
        }
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 400px;
            border-radius: 6px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }
        .close:hover {
            color: #333;
        }
        .error-message {
            color: #e74c3c;
            margin-top: 5px;
            font-size: 14px;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<h1>用户管理</h1>
<button onclick="loadUsers()">刷新用户列表</button>

<table id="userTable">
    <thead>
    <tr>
        <th>ID</th>
        <th>用户名</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>手机号</th>
        <th>邮箱</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <!-- 用户数据将动态填充到这里 -->
    </tbody>
</table>


<script>
    // 从 localStorage 中获取实际的 JWT 令牌
    const token = localStorage.getItem('token');
    // 替换为实际的后端 API 地址
    const apiBaseUrl = 'http://localhost:8080/api/admin';

    // 加载用户列表
    function loadUsers() {
        axios.get(`${apiBaseUrl}/users`, {
            headers: {
                Authorization: `Bearer ${token}`
            }
        })
            .then(response => {
                // 从响应对象中提取用户数组
                const users = response.data.data;

                // 验证数据是否为数组
                if (!Array.isArray(users)) {
                    throw new Error(`数据格式错误，应为数组，实际类型：${typeof users}`);
                }

                const tableBody = document.querySelector('#userTable tbody');
                tableBody.innerHTML = '';

                users.forEach(user => {
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>${user.id}</td>
                        <td>${user.account}</td>
                        <td>${user.name}</td>
                        <td>${user.age}</td>
                        <td>${user.phone}</td>
                        <td>${user.email}</td>
                        <td>
                            <button class="delete" onclick="deleteUser(${user.id})">删除</button>
                        </td>
                    `;
                    tableBody.appendChild(row);
                });
            })
            .catch(error => {
                let errorMsg = '加载用户列表失败，请检查网络连接';
                if (error.response) {
                    // 后端返回了错误响应
                    const { status, data } = error.response;
                    errorMsg = `错误码 ${status}: ${data.message || '接口返回异常数据'}`;
                } else if (error.request) {
                    // 请求发送成功但未收到响应
                    errorMsg = '请求超时，请重试';
                }
                console.error('加载用户列表失败:', error);
                alert(errorMsg);
            });
    }


    // 删除用户
    function deleteUser(userId) {
        if (confirm('确定要删除该用户吗？此操作不可恢复！')) {
            axios.delete(`${apiBaseUrl}/users/${userId}`, {
                headers: {
                    Authorization: `Bearer ${token}`
                }
            })
                .then(response => {
                    console.log('用户删除成功:', response.data);
                    loadUsers();
                    alert('用户删除成功');
                })
                .catch(error => {
                    let errorMsg = '用户删除失败，请稍后重试';
                    if (error.response) {
                        const { status, data } = error.response;
                        errorMsg = `错误码 ${status}: ${data.message || '删除失败'}`;
                    }
                    console.error('用户删除失败:', error);
                    alert(errorMsg);
                });
        }
    }

    // 页面加载时加载用户列表
    window.onload = loadUsers;
</script>
</body>
</html>